package com.sinoglobal.sinostore.widget;

import android.R.integer;
import android.content.Context;
import android.content.Loader;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;

/**
 * Found at http://stackoverflow.com/questions/7814017/is-it-possible-to-disable-scrolling-on-a-viewpager.
 * Convenient way to temporarily disable ViewPager navigation while interacting with ImageView.
 * 
 * Julia Zudikova
 */

/**
 * Hacky fix for Issue #4 and
 * http://code.google.com/p/android/issues/detail?id=18990
 * <p/>
 * ScaleGestureDetector seems to mess up the touch events, which means that
 * ViewGroups which make use of onInterceptTouchEvent throw a lot of
 * IllegalArgumentException: pointerIndex out of range.
 * <p/>
 * There's not much I can do in my code for now, but we can mask the result by
 * just catching the problem and ignoring it.
 *
 * @author Chris Banes
 */
public class HackyViewPager extends ViewPager {

	private boolean isLocked;
	
    public HackyViewPager(Context context) {
        super(context);
        isLocked = false;
    }

    public HackyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        isLocked = false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    	if (!isLocked) {
	        try {
	            return super.onInterceptTouchEvent(ev);
	        } catch (IllegalArgumentException e) {
	            e.printStackTrace();
	            return false;
	        }
    	}
    	return false;
    }

    int tempX,tempY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	int downX = 0,downY = 0,mTouchSlop=10;
    	switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			tempX=downX=(int) event.getX();
			tempY=downY=(int) event.getY();
			
			break;
		case MotionEvent.ACTION_MOVE:
			int moveX=(int) event.getX();
			int moveY=(int) event.getY();
			int deltaX = tempX - moveX;
			int deltaY = tempY - moveY;
			tempX=moveX;
			tempY=moveY;
//			if (Math.abs((int) event.getY() - downY)<Math.abs((int) event.getX() - downX)) {
			if (deltaX>deltaY) {
				getParent().requestDisallowInterceptTouchEvent(true);
			}
			//			boolean parentLocked=false;
//			if (Math.abs(deltaX)>Math.abs(deltaY)) {
//				parentLocked=true;
//			}
//		    if (getParent() instanceof View) {
//		    	ViewParent parentView=getParent();
//		    	while (parentView instanceof View) {
//		    		if (parentView instanceof MyScrollView) {
//		    			MyScrollView myScrollView=(MyScrollView) parentView;
//		    			myScrollView.setLocked(parentLocked);
//		    			break;
//					}
//		    		parentView=parentView.getParent();
//		    	}
//			}
    	}
        if (!isLocked) {
            return super.onTouchEvent(event);
        }
        return false;
    }
    
	public void toggleLock() {
		isLocked = !isLocked;
	}

	public void setLocked(boolean isLocked) {
		this.isLocked = isLocked;
	}

	public boolean isLocked() {
		return isLocked;
	}
	
}
